package com.falcon.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.UserTypePO;
import com.falcon.domain.dos.UserTypeDO;
import com.falcon.domain.req.UserTypeQuery;
import com.falcon.domain.req.UserTypeRequest;

import com.falcon.mapper.IUserTypeMapper;
import com.falcon.service.IUserTypeService;


@Slf4j
@Service
public class UserTypeService implements IUserTypeService {

    @Autowired
    private IUserTypeMapper userTypeMapper;

    @Override
    public Long save(UserTypeRequest request){
        UserTypePO userTypePO = BeanUtil.copyProperties(request, UserTypePO.class);

        this.userTypeMapper.insertNotNull(userTypePO);

        return userTypePO.getId();
    }

    @Override
    public Boolean update(Long id, UserTypeRequest request){

        UserTypePO userTypePO = BeanUtil.copyProperties(request, UserTypePO.class);
        userTypePO.setId(id);

        return this.userTypeMapper.updateNotNull(userTypePO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.userTypeMapper.deleteById(id);
    }

    @Override
    public UserTypeDO queryById(Long id){

        UserTypePO userTypePO = this.userTypeMapper.selectById(id);

        UserTypeDO userTypeDO = BeanUtil.copyProperties(userTypePO, UserTypeDO.class);

        return userTypeDO;
    }


    @Override
    public List<UserTypeDO> queryList(UserTypeQuery request){

        Condition condition = request.toCondition();

        List<UserTypePO> poList = this.userTypeMapper.selectList(condition);

        List<UserTypeDO> doList = BeanUtil.copyToList(poList, UserTypeDO.class);

        return doList;
    }

    @Override
    public BasePager<UserTypeDO> queryPager(UserTypeQuery request){


        PageInfo<UserTypePO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.userTypeMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, UserTypeDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.userTypeMapper.deleteByIds(ids);
    }


}
